package com.yvtian.om.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yvtian.om.pojo.OmOrderEntity;
import com.yvtian.om.dao.OmOrderMapper;
import com.yvtian.om.pojo.OmOrderShopEntity;
import com.yvtian.om.service.OmOrderService;
import com.yvtian.om.service.OmOrderShopService;
import com.yvtian.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

/**
 * (om_order)表服务实现类
 *
 * @author long
 * @since 2024-05-27 08:57:38
 */
@Service("omOrderService")
public class OmOrderServiceImpl extends ServiceImpl<OmOrderMapper, OmOrderEntity> implements OmOrderService {

    @Autowired
    private OmOrderShopService omOrderShopService;

    @Override
    public IPage<OmOrderEntity> findByPage(Page<OmOrderEntity> page, OmOrderEntity omOrderEntity) {
        LambdaQueryWrapper<OmOrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OmOrderEntity::getIsDel, 0);
        if (StringHelper.isNotEmpty(omOrderEntity.getCode())) {
            queryWrapper.like(OmOrderEntity::getCode, omOrderEntity.getCode());
        }
        if (StringHelper.isNotEmpty(omOrderEntity.getName())) {
            queryWrapper.like(OmOrderEntity::getName, omOrderEntity.getName());
        }
        if (StringHelper.isNotEmpty(omOrderEntity.getCuName())) {
            queryWrapper.like(OmOrderEntity::getCuName, omOrderEntity.getCuName());
        }
        if (StringHelper.isNotEmpty(omOrderEntity.getSellName())) {
            queryWrapper.like(OmOrderEntity::getSellName, omOrderEntity.getSellName());
        }
        if (omOrderEntity.getAmount() != null) {
            queryWrapper.eq(OmOrderEntity::getAmount, omOrderEntity.getAmount());
        }
        if (StringHelper.isNotEmpty(omOrderEntity.getProjectCode())) {
            queryWrapper.like(OmOrderEntity::getProjectCode, omOrderEntity.getProjectCode());
        }
        if (StringHelper.isNotEmpty(omOrderEntity.getProjectName())) {
            queryWrapper.like(OmOrderEntity::getProjectName, omOrderEntity.getProjectName());
        }
        if (omOrderEntity.getStatus() != null) {
            queryWrapper.eq(OmOrderEntity::getStatus, omOrderEntity.getStatus());
        }
        if (StringHelper.isNotEmpty(omOrderEntity.getCheckName())) {
            queryWrapper.like(OmOrderEntity::getCheckName, omOrderEntity.getCheckName());
        }
        return this.page(page, queryWrapper);
    }

    @Transactional
    @Override
    public boolean insertOrUpdateAll(OmOrderEntity omOrderEntity) {
        ArrayList<OmOrderShopEntity> omOrderShopList = omOrderEntity.getOmOrderShopList();
        if (omOrderShopList != null && !omOrderShopList.isEmpty()) {
            LambdaUpdateWrapper<OmOrderShopEntity> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(OmOrderShopEntity::getOrderCode, omOrderEntity.getCode());
            omOrderShopService.remove(updateWrapper);
            omOrderShopService.saveBatch(omOrderShopList);
        }
        this.saveOrUpdate(omOrderEntity);
        return true;
    }
}

